package ca.scotthyndman.as2haxe.dom;

import java.util.ArrayList;
import java.util.List;

import ca.scotthyndman.as2haxe.dom.visitor.IVisitor;

/**
 * Represents a switch statement.
 */
public class ASSwitchStatement extends ASStatement {

	/**
	 * The "expression" structural property of this node type.
	 */
	public static final ASChildPropertyDescriptor EXPRESSION_PROPERTY = new ASChildPropertyDescriptor(
			ASSwitchStatement.class, "expression", ASExpression.class, true);

	/**
	 * The "statements" structural property of this node type.
	 */
	public static final ASChildListPropertyDescriptor CASES_PROPERTY = new ASChildListPropertyDescriptor(
			ASSwitchStatement.class, "cases", ASSwitchCase.class);

	/**
	 * The switch expression.
	 */
	ASExpression expression;

	/**
	 * The list of switch cases.
	 */
	List<ASTNode> cases = new ASTNode.NodeList(CASES_PROPERTY);

	/**
	 * Constructs a new instance of ASSwitchStatement
	 * 
	 * @param ast
	 *            The abstract syntax tree that owns this element
	 */
	public ASSwitchStatement(AST ast) {
		super(ast);
	}

	//
	// ========
	//

	public ASExpression getExpression() {
		return expression;
	}

	public void setExpression(ASExpression expression) {
		if (expression == null) {
			throw new IllegalArgumentException();
		}
		ASTNode oldChild = this.expression;
		preReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
		this.expression = expression;
		postReplaceChild(oldChild, expression, EXPRESSION_PROPERTY);
	}

	public List<ASTNode> getCases() {
		return cases;
	}

	//
	// ======== INTERNAL
	//

	/*
	 * (omit javadoc for this method) Method declared on ASTNode.
	 */
	final ASTNode internalGetSetChildProperty(
			ASChildPropertyDescriptor property, boolean get, ASTNode child) {
		if (property == EXPRESSION_PROPERTY) {
			if (get) {
				return getExpression();
			} else {
				setExpression((ASExpression) child);
				return null;
			}
		}
		// allow default implementation to flag the error
		return super.internalGetSetChildProperty(property, get, child);
	}

	/*
	 * (omit javadoc for this method) Method declared on ASTNode.
	 */
	final List<ASTNode> internalGetChildListProperty(
			ASChildListPropertyDescriptor property) {
		if (property == CASES_PROPERTY) {
			return getCases();
		}
		// allow default implementation to flag the error
		return super.internalGetChildListProperty(property);
	}

	// ======== PROPERTIES

	/**
	 * A list of property descriptors (element type:
	 * {@link ASPropertyDescriptor}), or null if uninitialized.
	 */
	private static final List<ASPropertyDescriptor> PROPERTY_DESCRIPTORS;

	/**
	 * Constructs the properties
	 */
	static {
		List<Object> propertyList = new ArrayList<Object>(3);
		createPropertyList(ASSwitchStatement.class, propertyList);
		addProperty(EXPRESSION_PROPERTY, propertyList);
		addProperty(CASES_PROPERTY, propertyList);
		PROPERTY_DESCRIPTORS = reapPropertyList(propertyList);
	}

	/**
	 * Returns a list of structural property descriptors for this node type.
	 * Clients must not modify the result.
	 * 
	 * 
	 * @return a list of property descriptors (element type:
	 *         {@link ASPropertyDescriptor})
	 * 
	 */
	public static List<ASPropertyDescriptor> propertyDescriptors(int apiLevel) {
		return PROPERTY_DESCRIPTORS;
	}

	//
	// ======== OVERRIDES
	//

	@Override
	public void accept(IVisitor as2Visitor, boolean visitChilds) {
		visitChilds = as2Visitor.visit(this);

		if (!visitChilds) {
			return;
		}

		getExpression().accept(as2Visitor, visitChilds);
		for (ASTNode stmt : getCases()) {
			stmt.accept(as2Visitor, visitChilds);
		}
	}
}
